package mutsumimod.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.helpers.Hitbox;

public abstract class IconUi {

    public Texture img;
    public Hitbox hb;
    public Sprite iconSprite;
    public float animationTime;
    public float swingTime;
    public static final float SCALE_DURATION = 0.2f;
    public static final float SWING_SPEED = 8f;
    public static final float MAX_SCALE = 1.2f;
    public static final float MAX_ROTATION = 5f;

    public IconUi(Texture img, float x, float y) {
        hb = new Hitbox(70.0F * Settings.scale, 70.0F * Settings.scale);
        hb.move(Settings.WIDTH - x * Settings.scale, Settings.HEIGHT - y * Settings.scale);
        this.img=img;
        setIconSprite();
    }

    public void setIconSprite() {
        iconSprite = new Sprite(img);
        iconSprite.setOriginCenter();
        iconSprite.setPosition(hb.cX - iconSprite.getWidth() / 2, hb.cY - iconSprite.getHeight() / 2);
        iconSprite.setSize(70f * Settings.scale, 70f * Settings.scale);
    }

    public void render(SpriteBatch sb) {
        sb.setColor(Color.WHITE);
        iconSprite.draw(sb);
        hb.render(sb);
    }

    public void update() {
        updateAnimation();
    }

    private void updateAnimation() {
        float deltaTime = Gdx.graphics.getDeltaTime();

        if (hb.hovered) {
            animationTime += deltaTime / SCALE_DURATION;
            swingTime += deltaTime * SWING_SPEED;
        } else {
            animationTime -= deltaTime / SCALE_DURATION;
            swingTime = 0f;
        }

        animationTime = MathUtils.clamp(animationTime, 0f, 1f);

        float currentScale = 1 + (MAX_SCALE - 1) * animationTime;

        iconSprite.setScale(currentScale);
        iconSprite.setPosition(hb.cX - iconSprite.getWidth() / 2, hb.cY - iconSprite.getHeight() / 2);
    }

}